package me.sealer.algorithm.utils.string;

/**
 * Created by sealer on 17/04/17.
 */
public class BF {
    public static int bf(String target, String pattern) {
        if (isNull(target) || isNull(pattern)) {
            return -1;
        }

        int tLength = target.length();
        int pLength = pattern.length();
        for (int i = 0; i <= tLength - pLength; i++) {
            int j = 0;
            while (j < pLength && target.charAt(i + j) == pattern.charAt(j)) {
                j++;
            }

            if (j >= pLength) {
                return i;
            }

        }

        return -1;
    }

    public static boolean isNull(String s) {
        return s == null || "".equals(s);
    }
}
